//
//  SpaceEditPopupView.swift
//  aj
//
//  Created by leeqb on 2/28/16.
//  Copyright © 2016 powerlee. All rights reserved.
//

import Foundation

@objc protocol SpaceEditPopupViewDelegate {
    optional func spaceEditSuccess(responseObject: AnyObject)
}

class SpaceEditPopupView: UIView {
    var delegate: SpaceEditPopupViewDelegate!
    var popWin: UIWindow!
    
    @IBOutlet var contentView: UIView!
    @IBOutlet weak var nameField: UITextField!
    @IBOutlet weak var faceField: UITextField!
    @IBOutlet weak var descField: UITextView!
    
    var spaceInfo: NSDictionary! {
        didSet {
            nameField.text = spaceInfo["zoneName"] as? String
            faceField.text = spaceInfo["zoneUrl"] as? String
            //descField.text = spaceInfo["zoneUrl"] as? String
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initSelf()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.initSelf()
    }
    
    private func initSelf() {
        LX("SpaceEditPopupView", owner: self)
        
        nameField.layer.borderWidth = 0.5
        nameField.layer.borderColor = RGB(237, g: 237, b: 237).CGColor
        faceField.layer.borderWidth = 0.5
        faceField.layer.borderColor = RGB(237, g: 237, b: 237).CGColor
        descField.layer.borderWidth = 0.5
        descField.layer.borderColor = RGB(237, g: 237, b: 237).CGColor
        
        popWin = UIWindow(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
        popWin.windowLevel = UIWindowLevelAlert
        
        let blackAlphaView = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
        blackAlphaView.backgroundColor = UIColor.blackColor()
        blackAlphaView.alpha = 0.5
        popWin.addSubview(blackAlphaView)
        popWin.addSubview(contentView)
        
        contentView.frame = CGRect(x: 20, y: (SCREEN_HEIGHT - 240) / 2, width: SCREEN_WIDTH - 40, height: 240)
    }
    
    func show() {
        popWin.makeKeyAndVisible()
    }
    
    func dismiss() {
        popWin.resignKeyWindow()
        popWin = nil
    }
    
    @IBAction func sureClick(sender: UIButton) {
        let name = nameField.text!
        let cover = faceField.text!
        let desc = descField.text!
        
        if name.isEmpty {
            CBAlertView.shareInstance().showMsg("请输入空间名称")
            return
        }
        
        if cover.isEmpty {
            CBAlertView.shareInstance().showMsg("请选择封面")
            return
        }
        
        if cover.isEmpty {
            CBAlertView.shareInstance().showMsg("请选择空间封面")
            return
        }
        
        let params = ["oper": "2", "albumId":spaceInfo["zoneId"]!, "albumName": name, "albumUrl": cover, "albumDesc": desc, "albumType": "2"]
        AJNetworkHelper.instance.post("aj_album_mgr", parameters: params, success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) -> Void in
            print(responseObject)
            if responseObject["returnCode"] as? String == RetCodeSuccess {
                if self.delegate != nil && self.delegate.spaceEditSuccess != nil {
                    self.delegate.spaceEditSuccess!(responseObject)
                }
                
                CBAlertView.shareInstance().showMsg("保存成功")
                self.dismiss()
            } else {
                CBAlertView.shareInstance().showMsg(responseObject["errorMsg"] as? String)
            }
            }, failed: { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
                print(error)
            }) { () -> Void in
        }
    }
    
    @IBAction func cancelClick(sender: UIButton) {
        self.dismiss()
    }
}